package com.example.towSpringBoot.controller;

import com.example.towSpringBoot.common.Result;
import com.example.towSpringBoot.dto.EmployeeDto;
import com.example.towSpringBoot.dto.LoginDto;
import com.example.towSpringBoot.dto.RandomNumDto;
import com.example.towSpringBoot.service.LoginService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description: 登录、登出、验证码
 * @author: xinTong
 * @create: 2024-01-24 16:26
 **/
@RestController
@RequestMapping("api")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 验证码和图片
     * @return
     */
    @GetMapping("/code")
    public Result code(){
        RandomNumDto randomNumDto = loginService.code();
        return Result.success(randomNumDto);
    }

    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto) throws JsonProcessingException {
        EmployeeDto employeeDto = loginService.select(loginDto);
        return Result.success("登录成功",employeeDto);
    }

    @GetMapping("logout")
    public Result logout(HttpServletRequest request){
        loginService.loginOut(request);
        return Result.success("退出成功",null);
    }
}
